package com.inossem.warehousemanage_tv.fragment;

import android.widget.GridView;

import com.google.gson.Gson;
import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.adapter.ShelvesAdapter;
import com.inossem.warehousemanage_tv.bean.BaseBean;
import com.inossem.warehousemanage_tv.bean.PositionsBean;
import com.inossem.warehousemanage_tv.bean.ShelvesBean;
import com.inossem.warehousemanage_tv.utils.Api;
import com.inossem.warehousemanage_tv.utils.ToastUtils;
import com.inossem.warehousemanage_tv.utils.requestUtils.InossemCallback;
import com.inossem.warehousemanage_tv.utils.requestUtils.RetrofitUtils;

import java.util.Collections;
import java.util.List;

import retrofit2.Response;

/**
 * 仓库概览-货架
 *
 * @author 詹建宇
 * @time on 2019/2/13 17:51
 * @email jianyu.zhan@inossem.com
 */
public class WareShelvesFragment extends BaseFragment {

    private GridView gridView;
    private List<ShelvesBean.ShelvesListBean> list;

    @Override
    public int initLayout() {
        return R.layout.fragment_ware_shelves;
    }

    @Override
    public void initView() {
        gridView = mView.findViewById(R.id.gridView);
    }

    @Override
    public void initData() {

        RetrofitUtils.getRetrofit(mActivity, "詹建宇", "刘宇", "仓库概览-货架", "通过库存地点获取货架")
                .create(Api.class)
                .getShelvesList(mActivity.location_id)
                .enqueue(new InossemCallback<BaseBean<ShelvesBean>>() {
                    @Override
                    public void success(Response<BaseBean<ShelvesBean>> response) {
                        ShelvesBean bean = response.body().getBody();
                        mActivity.location_id = bean.getLocation_id();
                        list = bean.getShelves_list();
                        Collections.sort(list);
                        if (null != list && !list.isEmpty())
                            gridView.setAdapter(new ShelvesAdapter(mActivity, list));
                    }

                    @Override
                    public void failure(Response<BaseBean<ShelvesBean>> response) {

                    }
                });

    }

    @Override
    public void initClick() {
        gridView.setOnItemClickListener((parent, view, position, id) -> {
            String is_temporar_area = list.get(position).getIs_temporar_area();
            String is_click = list.get(position).getIs_click();
            mActivity.area_id = list.get(position).getArea_id();
            mActivity.position_index1 = list.get(position).getPosition_index1();
            mActivity.shelveName = list.get(position).getArea_name() + "(" + list.get(position).getArea_code() + "-" + list.get(position).getPosition_index1() + ")";
            if ("1".equals(is_click)) {
                if ("1".equals(is_temporar_area)) {
                    mActivity.isTemporaryShelves = true;
                    RetrofitUtils.getRetrofit(mActivity, "詹建宇", "刘宇", "仓库概览-仓位", "通过货架获取仓位")
                            .create(Api.class)
                            .getPositionList(mActivity.location_id, mActivity.area_id, mActivity.position_index1)
                            .enqueue(new InossemCallback<BaseBean<PositionsBean>>() {
                                @Override
                                public void success(Response<BaseBean<PositionsBean>> response) {
                                    try {
                                        mActivity.position_id = response.body().getBody().getLayer_list().get(0).getPosition_list().get(0).getPosition_id();
                                        mActivity.isTemporary = false;
                                        mActivity.startFragment(new WareMatsFragment());
                                    } catch (Exception ignored) {
                                        ToastUtils.showToast("此待上架区没有物料");
                                    }
                                }

                                @Override
                                public void failure(Response<BaseBean<PositionsBean>> response) {

                                }
                            });
                } else {
                    mActivity.isTemporaryShelves = false;
                    mActivity.position_index1 = list.get(position).getPosition_index1();
                    mActivity.area_id = list.get(position).getArea_id();
                    mActivity.startFragment(new WarePositionsFragment());
                }
            } else {
                if ("1".equals(is_temporar_area)) {
                    ToastUtils.showToast("此临时存储区下没有物料");
                } else {
                    ToastUtils.showToast("此货架下没有物料");
                }
            }
        });
    }

}
